package com.fejal.dao;

import java.util.List;
import com.fejal.bean.AlunoCracha;

public class AlunoCrachaDAOHibernate implements AlunoCrachaDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<AlunoCracha> hibernateDAO = null;

	public AlunoCrachaDAOHibernate() {
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getAlunoCrachaDao();
	}

	@Override
	public boolean inserir(AlunoCracha alunoCracha) throws Exception {

		boolean ok = false;

		try {
			hibernateDAO.add(alunoCracha);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}

		return ok;
	}

	@Override
	public boolean alterar(AlunoCracha alunoCracha) throws Exception{

		boolean ok = false;
	
		
		try {
			hibernateDAO.update(alunoCracha);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@Override
	public boolean excluir(AlunoCracha alunoCracha) throws Exception{

		boolean ok = false;

		try {
			hibernateDAO.remove(alunoCracha);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 
		return ok;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AlunoCracha> getSelecionar(String matricula, String nome){
		String 	sql = "FROM alunoCracha WHERE codigoAlunoCracha <> 0 ";
/*
		if (!(descricao.isEmpty())) {
			sql += "AND descricao like '" + descricao + "%' ";  
		}
		sql += "ORDER BY descricao";	
*/	
 
		List<AlunoCracha> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}

	@Override
	public AlunoCracha getAlunoCracha(int codigoAlunoCracha) {
		
		AlunoCracha unidade = hibernateDAO.get(codigoAlunoCracha);
		return unidade;
	}

	@Override
	public List<AlunoCracha> getListTodos() {
		
		List<AlunoCracha> list = hibernateDAO.listAll();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isExiste(AlunoCracha alunoCracha) {
		// SQL para verificar se a Unidade existe
				
		String 	sql = 	"";

		List<AlunoCracha> lista = hibernateDAO.getSession().createQuery(sql).list();
		if(lista.size()<1){
			return false;
		} else {
			return true;
		}
	}

}
